package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.TlCart;
import com.qf.entity.TlGoods;
import com.qf.entity.TlUser;
import com.qf.mapper.CartMapper;
import com.qf.mapper.GoodsMapper;
import com.qf.service.CartService;
import com.qf.service.UserService;
import com.qf.utils.Mycode;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, TlCart> implements CartService {

    @Autowired(required = false)
    CartMapper cartMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired(required = false)
    UserService userService;

    @Autowired(required = false)
    GoodsMapper goodsMapper;

    @Override
    public ResultMsg findAllByUid(HttpServletRequest request) {
        ResultMsg resultMsg = userService.user(request);
        TlUser user = (TlUser) resultMsg.getData();
        List<TlCart> allByUid = cartMapper.findAllByUid(user.getUserId());
        return ResultMsg.SUCCESS(allByUid);
    }

    @Override
    public ResultMsg addCart(Integer gid,Integer num,HttpServletRequest request) {
        //先判断购物车中是否有该商品
        QueryWrapper<TlCart> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",gid);
        TlCart one = super.getOne(wrapper);
        if (one!=null){
            one.setCartNum(one.getCartNum()+num);
            super.saveOrUpdate(one);
            return ResultMsg.SUCCESS();
        }
        ResultMsg resultMsg = userService.user(request);
        TlUser user = (TlUser) resultMsg.getData();
        TlGoods tlGoods = goodsMapper.selectById(gid);
        TlCart tlCart = new TlCart();
        tlCart.setUserId(user.getUserId());
        tlCart.setGoodsId(gid);
        tlCart.setCartGprice(tlGoods.getGoodsPrice());
        tlCart.setCartGname(tlGoods.getGoodsName());
        tlCart.setCartGpic(tlGoods.getGoodsPic1());
        tlCart.setCartNum(num);
        boolean save = super.save(tlCart);
        if (save)
            return ResultMsg.SUCCESS();
        return ResultMsg.ERROR(Mycode.ADDCART_ERRO);
    }

    @Override
    public ResultMsg deleteOne(Integer gid) {
        QueryWrapper<TlCart> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",gid);
        boolean remove = super.remove(wrapper);
        if (remove)
            return ResultMsg.SUCCESS();
        return ResultMsg.ERROR(Mycode.DELETEONE_ERRO);
    }

    @Override
    public ResultMsg deleteAll(HttpServletRequest request) {
        //获取登录用户的信息
        ResultMsg resultMsg = userService.user(request);
        TlUser user = (TlUser) resultMsg.getData();
        QueryWrapper<TlCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        boolean remove = super.remove(queryWrapper);
        if (remove)
            return ResultMsg.SUCCESS();
        return ResultMsg.ERROR(Mycode.DELETEALL_ERRO);
    }
}
